package gui.components;

import java.util.Date;
import java.util.LinkedList;

import producedepartment.bean.ProduceHistoryBean;
import producer.impl.Producer;
import lombok.Setter;
/**
 * Okno dialogowe dla histori produkcji
 * @author Witold Muszynski
 *
 */
public class HistoryDialog extends javax.swing.JDialog {

    /** Creates new form History */
    public HistoryDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBounds(new java.awt.Rectangle(400, 200, 0, 0));

        
        jScrollPane1.setAutoscrolls(true);
        jScrollPane1.setEnabled(false);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setFocusable(false);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Historia zamowien");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

 public void setLabelText(String s){
	 jLabel1.setText(s);
 }
 
 public void setAreaText(String s){
	 jTextArea1.setText(s);
 }
 
 public void addAreaText(String s){
	 jTextArea1.setText(jTextArea1.getText()+s);
 }
 
 public void setHistory(String term){
	 setAreaText("");
	 if (producer == null) return;
	 historyBeans = producer.getManagment().getProduceDepartment().getProduceHistory().getHistory();
	 
	 now = new Date();
	 
	 switch (term){
	 case "Dzienna" : backTime = 30;
		 			break;
	 case "Tygodniowa" : backTime = 90;
		 			break;
	 case "Miesieczna" : backTime = 300;
		 			break;
	 case "Roczna" : backTime = 900;
		 			break;
	 }
	 
	 limitTime = now.getTime() - backTime * 1000;
	 
	 for(ProduceHistoryBean phb : historyBeans){
		 if (phb.getProductData().getTime() >= limitTime) {
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append("Czas: ");
			stringBuilder.append(phb.getProductData().toLocaleString());
			stringBuilder.append(" Nazwa produktu: ");
			stringBuilder.append(phb.getName());
			stringBuilder.append(" Kategoria: ");
			stringBuilder.append(phb.getCategory());
			stringBuilder.append(" Ilosc: ");
			stringBuilder.append(phb.getCount());
			stringBuilder.append("\n");
			addAreaText(stringBuilder.toString());
		}
			 
	 }
 }
 
 public void addProducerToArea(Producer producer){
	 
	 if (producer == null) return;
	 historyBeans = producer.getManagment().getProduceDepartment().getProduceHistory().getHistory();
	 String producerName = producer.getCompanyName();
	 
	 for(ProduceHistoryBean phb : historyBeans){
		 if (phb.getProductData().getTime() >= limitTime) {
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append(producerName);
			stringBuilder.append("");
			stringBuilder.append(" || Czas: ");
			stringBuilder.append(phb.getProductData().toLocaleString());
			stringBuilder.append(" Nazwa produktu: ");
			stringBuilder.append(phb.getName());
			stringBuilder.append(" Kategoria: ");
			stringBuilder.append(phb.getCategory());
			stringBuilder.append(" Ilosc: ");
			stringBuilder.append(phb.getCount());
			stringBuilder.append("\n");
			addAreaText(stringBuilder.toString());
		}
			 
	 }
 }
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    
    @Setter
    private Producer producer;
    LinkedList<ProduceHistoryBean> historyBeans;
    private Date now;
    private int backTime;
    private long limitTime;
    // End of variables declaration
}